package com.webchat.ugc.service;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.AccountRelationTypeEnum;
import com.webchat.common.enums.RoleCodeEnum;
import com.webchat.common.enums.mall.MallAccountTypeEnum;
import com.webchat.domain.vo.request.AccountRelationQueryDTO;
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
import com.webchat.domain.vo.response.mall.im.MallChatAccountBaseVO;
import com.webchat.rmi.user.UserServiceClient;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AccountService {

    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 走账号基础微服务，检验当前account是否人员账号
     *
     * @param account
     * @return
     */
    public boolean isUserAccount(String account) {

        UserBaseResponseInfoVO user = this.accountInfo(account);
        return user != null && RoleCodeEnum.isUserRole(user.getRoleCode());
    }

    public List<MallChatAccountBaseVO> batchGetMallAccount(List<String> accounts) {

        List<UserBaseResponseInfoVO> batchGet = this.batchGet(accounts);
        if (CollectionUtils.isEmpty(batchGet)) {
            return Collections.emptyList();
        }
        return batchGet.stream().map(u ->  {
            MallChatAccountBaseVO mallChatAccountBaseVO = new MallChatAccountBaseVO();
            mallChatAccountBaseVO.setAccount(u.getUserId());
            mallChatAccountBaseVO.setAccountName(u.getUserName());
            mallChatAccountBaseVO.setAccountLogo(u.getPhoto());
            mallChatAccountBaseVO.setAccountType(MallAccountTypeEnum.BUYER.getType());
            return mallChatAccountBaseVO;
        }).toList();
    }

    /**
     * 查询当前账号信息
     *
     * @param account
     * @return
     */

    public UserBaseResponseInfoVO accountInfo(String account) {

        APIResponseBean<UserBaseResponseInfoVO> responseBean = userServiceClient.userInfo(account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }

    /**
     *  批量查询账号详情数据
     *
     * @param accounts
     * @return
     */
    public List<UserBaseResponseInfoVO> batchGet(List<String> accounts) {
        if (CollectionUtils.isEmpty(accounts)) {
            return Collections.emptyList();
        }
        APIResponseBean<List<UserBaseResponseInfoVO>> responseBean =
                userServiceClient.batchGet(accounts);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new RuntimeException(responseBean.getMsg());
    }

    public Map<String, UserBaseResponseInfoVO> batchGet(Set<String> accounts) {
        if (CollectionUtils.isEmpty(accounts)) {
            return Collections.emptyMap();
        }
        APIResponseBean<Map<String, UserBaseResponseInfoVO>> responseBean =
                userServiceClient.batchGet(accounts);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new RuntimeException(responseBean.getMsg());
    }

    /**
     * 获取群组下的群成员用户id集合
     *
     * @param groupAccount
     * @return
     */
    public Set<String> getGroupUserIds(String groupAccount) {
        APIResponseBean<Set<String>> responseBean = userServiceClient.getAllSubscriberByAccount(
                AccountRelationTypeEnum.USER_GROUP.getType(), groupAccount);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }

    /**
     * 获取群组下的群成员用户id集合
     *
     * @return
     */
    public Set<String> getAllSubscriberByAccount(String account, AccountRelationTypeEnum accountRelationType) {
        APIResponseBean<Set<String>> responseBean =
                userServiceClient.getAllSubscriberByAccount(accountRelationType.getType(), account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }


    /**
     * 判断userAccount是否订阅account
     *
     * @param userAccount
     * @param account
     * @param accountRelationType
     * @return
     */
    public boolean isSubscribe(String userAccount, String account, AccountRelationTypeEnum accountRelationType) {
        APIResponseBean<Boolean> responseBean =
                userServiceClient.isSubscribe(accountRelationType.getType(), userAccount, account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        return false;
    }

    /**
     * 批量查询账号间的订阅关系
     *
     * @param userAccount
     * @param accounts
     * @param accountRelationType
     * @return
     */
    public Map<String, Boolean> isBatchSubscribe(String userAccount, List<String> accounts, AccountRelationTypeEnum accountRelationType) {

        AccountRelationQueryDTO accountRelationQuery = AccountRelationQueryDTO.builder()
                                                        .sourceAccountIds(accounts)
                                                        .targetAccountId(userAccount)
                                                        .relationType(accountRelationType.getType())
                                                        .build();
        APIResponseBean<Map<String, Boolean>> responseBean =
                userServiceClient.batchIsSubscribe(accountRelationQuery);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return Collections.emptyMap();
    }
}
